import { createSlice } from "@reduxjs/toolkit";
import { RoleDataType } from ".";
const inistate = {
    roleMsg:[] as RoleDataType[]
};

const roleSlice = createSlice({
  name: "role",
  initialState: inistate,
  reducers: {
    Set: (state, action) => {
      state.roleMsg = []
      state.roleMsg = action.payload
    },

    Add: (state, action) => {
      console.log(action);
      state.roleMsg.push(action.payload);
    },
    Modify: (state, action) => {
      console.log(action);
      state.roleMsg = state.roleMsg.map((role) =>
        role.id === action.payload.id
          ? {
              ...role,
              name: action.payload.name,
              description: action.payload.description,
            }
          : role
      );
    },


    Delete: (state, action) => {
      state.roleMsg  = state.roleMsg.filter((role) => role.id !== action.payload);
    },
  },
});

export const { Add, Modify, Delete, Set } = roleSlice.actions;

export default roleSlice.reducer;
